/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: MainController
 * @Package com.rx.controller
 * @Description: 主要请求
 * @author: 陈锦韬
 * @date: 2022/2/26
 * @version V1.0
 * @Copyright: 2022 陈锦韬  All rights reserved.
 */
package com.rx.controller;

import cn.hutool.core.io.FileUtil;
import com.rx.bean.RxMainConfigBean;
import com.rx.core.base.RxBaseConstant;
import com.rx.core.base.RxBaseController;
import com.rx.core.util.RxUtil;
import com.rx.service.MainService;
import com.rx.vo.MainHelloVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: CHENJINTAO170
 * @Description: 主要请求
 * @date: 2022/2/26
 */
@RestController
@RequestMapping("system")
@Slf4j
public class MainController extends RxBaseController {

    @Autowired
    private MainService mainService;
    @Autowired
    private RxMainConfigBean rxMainConfigBean;
    /**
     * 开机询问。获取是否播放广告。最新版本号。广告文件名。
     * @param mainHelloVo
     * @return
     */
    @PostMapping("/hello")
    public MainHelloVo hello(@RequestBody MainHelloVo mainHelloVo){
        return  mainService.hello(mainHelloVo);
    }

    @RequestMapping("/download")
    public void download(@RequestParam String fileName,@RequestParam String sequences, HttpServletRequest request,HttpServletResponse response) {

        SimpleDateFormat formatter = new SimpleDateFormat(RxBaseConstant.DATE_FORMAT_DATETIME.trim());
        String dateString = formatter.format(new Date()).trim();
        String md5 = RxUtil.EncryptUtil.toMD5(dateString.substring(0,16));
        if(!md5.equalsIgnoreCase(sequences)){
           // throw new RxBaseException("999999","错误");
        }

        downloadFile(response, fileName, !request.getHeader("User-Agent").contains("Trident"));
    }

    public String downloadFile(HttpServletResponse response, String fileName, boolean notIE) {
        File file = new File(rxMainConfigBean.getFilePath() + fileName);

        FileInputStream stream = null;
        try {
            stream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //InputStream stream = FileUtil.class.getClassLoader().getResourceAsStream("app/" + fileName);

        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        try {
            String name = java.net.URLEncoder.encode(fileName, "UTF-8");
            if (notIE) {
                name = java.net.URLDecoder.decode(name, "ISO-8859-1");
            }
            response.setHeader("Content-Disposition", "attachment;filename=" + name );
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
        }

        log.debug("开始");
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            bis = new BufferedInputStream(stream);

            int total = 0;
            int length;
            while ((length = bis.read(buff)) != -1) {
                // 后面的length 关键
                os.write(buff, 0, length);
                os.flush();
                total += length;
            }
            System.out.println("total:" + total);
        } catch (FileNotFoundException e1) {
            //e1.getMessage()+"系统找不到指定的文件";
            return "系统找不到指定的文件";
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "success";
    }

}
